package lotterysystem.lotterysystem.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lotterysystem.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import lotterysystem.lotterysystem.common.exception.ControllerException;
import lotterysystem.lotterysystem.common.pojo.CommonResult;
import lotterysystem.lotterysystem.common.utils.JacksonUtil;
import lotterysystem.lotterysystem.controller.param.CreatePrizeParam;
import lotterysystem.lotterysystem.controller.param.PageParam;
import lotterysystem.lotterysystem.controller.result.FindPrizeListResult;
import lotterysystem.lotterysystem.service.PictureService;
import lotterysystem.lotterysystem.service.PrizeService;
import lotterysystem.lotterysystem.service.dto.PageListDTO;
import lotterysystem.lotterysystem.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeController {

    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);

    @Resource
    private PictureService pictureService;

    @Resource
    private PrizeService prizeService;

    /**
     * 上传奖品图片
     * @param file 图片
     * @return String
     */
    @PostMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * @param param 奖品信息的表单
     * @param picFile 图片
     * @return CommonResult
     */
    @PostMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile) {
        //打印日志
        logger.info("createPrize CreatePrizeParam:{}", JacksonUtil.writeValueAsString(param));
        //调用Service
        return CommonResult.success(prizeService.createPrize(param,picFile));
    }

    @GetMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {

        //打印日志
        logger.info("findPrizeList PageParam:{}",JacksonUtil.writeValueAsString(param));
        //调用Service
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {

        //非空校验
        if (null == pageListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(pageListDTO.getRecords().stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    return prizeInfo;
                }).collect(Collectors.toList())
        );

        return result;
    }
}
